using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.ServiceModel.Syndication;
using WebCore;
using WebCore.Helpers;
using DomainModel.Abstract;
using DomainModel.Concrete;
using System.Web.Routing;
using WebCore.Caching;


namespace WebController.Controllers
{
    public class RssController : Controller
    {
        INewsRepository newsRepository = new NewsRepository();
        IPostsRepository postsRepository = new PostsRepository();

        #region News RSS
        public ActionResult News()
        {
            string key = string.Format(Settings.Cache_RSS__Key, "news");

            object rssObject = WebCache.Get(key);
            if (Settings.CacheEnabled && rssObject != null)
            {
                return new Rss((SyndicationFeed)rssObject);
            }
            
            SyndicationFeed feed = new SyndicationFeed(Settings.RssNewsTitle, Settings.RssNewsDescription, Request.Url, GetNewsRssItems());

            // add cache
            WebCache.Add(key, feed);

            return new Rss(feed);
        }

        public IList<SyndicationItem> GetNewsRssItems()
        {
            IList<SyndicationItem> items = new List<SyndicationItem>();
            string url = string.Empty;

            foreach (var item in newsRepository.GetTop(Settings.NumberOfRssItems).ToList())
            {
                url = Url.GetBaseUrl();
                url += Url.RouteUrl(new { controller = "News", action = "Details", id = item.NewsID, title = Utils.RemoveIllegalCharacters(item.Title, true) });
                //TODO: Check the different between RouteUrl and Action
                //url = Url.Action("Details", "News", new RouteValueDictionary(new { id = item.NewsID, title = Utils.RemoveIllegalCharacters(item.Title) }));
                items.Add(new SyndicationItem(item.Title, item.Description, new Uri(url), null, item.DateUpdated));
            }
            return items;
        }

        #endregion 

        #region Blog RSS

        public ActionResult Blog()
        {

            string key = string.Format(Settings.Cache_RSS__Key, "blog");

            object rssObject = WebCache.Get(key);
            if (Settings.CacheEnabled && rssObject != null)
            {
                return new Rss((SyndicationFeed)rssObject);
            }
            
            SyndicationFeed feed = new SyndicationFeed(Settings.RssBlogTitle, Settings.RssBlogDescription, Request.Url, GetBlogRssItems());

            // add cache
            WebCache.Add(key, feed);

            return new Rss(feed);
        }

        public IList<SyndicationItem> GetBlogRssItems()
        {
            IList<SyndicationItem> items = new List<SyndicationItem>();
            string url = string.Empty;

            foreach (var item in postsRepository.GetTop(Settings.NumberOfRssItems).ToList())
            {
                url = Url.GetBaseUrl();
                url += Url.RouteUrl(new { controller = "Posts", action = "Details", id = item.PostID, title = Utils.RemoveIllegalCharacters(item.Title, true) });
                //TODO: Check the different between RouteUrl and Action
                //url = Url.Action("Details", "News", new RouteValueDictionary(new { id = item.NewsID, title = Utils.RemoveIllegalCharacters(item.Title) }));
                items.Add(new SyndicationItem(item.Title, item.Description, new Uri(url), null, item.DateUpdated));
            }
            return items;
        }

        #endregion 
    }
}
